<template>
  <div class="reset-container">
    <el-card title="密码重置" class="reset-card">
      <el-form ref="resetForm" :model="resetForm" :rules="rules" label-width="100px">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="resetForm.email" placeholder="请输入注册邮箱"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-row :gutter="10">
            <el-col :span="16">
              <el-input v-model="resetForm.code" placeholder="请输入验证码"></el-input>
            </el-col>
            <el-col :span="8">
              <el-button
                  type="primary"
                  @click="sendCode"
                  :disabled="countdown > 0"
              >
                {{ countdown > 0 ? `${countdown}秒后重发` : '发送验证码' }}
              </el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="resetForm.newPassword" type="password" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReset">重置密码</el-button>
          <el-button @click="$router.push('/')" style="margin-left: 10px;">返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
// 注意：这里使用 $router 而不是 useRouter
// import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

// const router = useRouter()
const resetForm = ref({
  email: '',
  code: '',
  newPassword: ''
})
const countdown = ref(0) // 验证码倒计时

// 表单验证规则
const rules = ref({
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
})

// 发送验证码
const sendCode = () => {
  if (!resetForm.value.email) {
    ElMessage.warning('请先输入邮箱')
    return
  }
  // 调用后端发送验证码接口
  // 注意：这里暂时注释掉实际的API调用，因为需要后端支持
  /*
  $axios.post('/send-reset-code', { email: resetForm.value.email })
      .then(response => {
        if (response.data.code === 200) {
          ElMessage.success('验证码已发送至邮箱')
          // 开始倒计时
          countdown.value = 60
          const timer = setInterval(() => {
            countdown.value--
            if (countdown.value <= 0) {
              clearInterval(timer)
            }
          }, 1000)
        } else {
          ElMessage.error(response.data.msg)
        }
      })
      .catch(error => {
        ElMessage.error('发送失败，请检查邮箱是否正确')
        console.error(error)
      })
  */
  ElMessage.info('演示模式：验证码功能需要后端支持')
}

// 重置密码
const handleReset = () => {
  // 调用后端重置密码接口
  // 注意：这里暂时注释掉实际的API调用，因为需要后端支持
  /*
  $axios.post('/reset-password', resetForm.value)
      .then(response => {
        if (response.data.code === 200) {
          ElMessage.success('密码重置成功，请登录')
          router.push('/') // 跳回登录页
        } else {
          ElMessage.error(response.data.msg)
        }
      })
      .catch(error => {
        ElMessage.error('重置失败，请检查信息')
        console.error(error)
      })
  */
  ElMessage.info('演示模式：密码重置功能需要后端支持')
}
</script>

<style scoped>
.reset-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}
.reset-card {
  width: 500px;
  padding: 20px;
}
</style>